<?php

/**
 * Class LabelModule
 * 数据表 id name
 */
class LabelModule extends SiteMode {
    /** @return LabelModule */
    public static function init() {
        return parent::init();
    }

    // $ids格式为 1 或者 1,2
    public function getDetail($ids){
        $sql="select * from `goods_label` where ".$this->db->getIdCondition($ids);
        $data=$this->db->getAll($sql);
        return is_array($ids)? $data: reset($data);
    }
    //搜出所有标签
    public function getList($rows=30,$offset=0){
        $rows=(int)$rows;
        $offset=(int)$offset;
        $sql="SELECT * FROM `goods_label` where site_id=? limit $offset,$rows";
        return $this->db->getList($sql,$this->siteId);
    }
    //搜出商品具有的标签
    public function getListByIds($ids){
        if(empty($ids)){
            return array();
        }
        $sql="select * from `goods_label` where FIND_IN_SET(id,?) and site_id=?";
        return $this->db->getAll($sql, $ids, $this->siteId);
    }

    public function add($name){
        $data['name']=$name;
        $data['site_id']=$this->siteId;
        if($this->db->insert('goods_label',$data) == 1){
            return (int)$this->db->insertId();
        }
        return -1;
    }
    public function checkExist($name){
        $sql="SELECT 1 FROM `goods_label` WHERE `name`=? and site_id=?";
        return $this->db->getExist($sql,$name,$this->siteId);
    }
    public function delete($id){
        $id=(int)$id;
        return $this->db->delete('goods_label',$id)==1;
    }
    public function modify($id,$name){
        if($this->checkExist($name)){
            return "该标签名已经存在了，请勿重复定义";
        }
        $id=(int)$id;
        $data['name']=$name;
        if($this->db->update('goods_label',$id,$data)){
            return 'success';
        }else{
            return "修改失败";
        }
    }
} 